package com.slm.report.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.slm.report.databinding.AdapterHeadViewBinding;
import com.slm.report.db.ContentDataBean;
import com.slm.report.db.ContentType;
import com.slm.report.view.data.BaseContentView;
import com.slm.report.view.data.ChoiceContentView;
import com.slm.report.view.data.EditContentView;
import com.slm.report.view.data.HistoryVisionContentView;
import com.slm.report.view.data.RadioContentView;
import com.slm.report.view.data.SchoolChoiceContentView;
import com.slm.report.view.data.TimeContentView;
import com.slm.report.view.data.VisionContentView;

import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.adapter
 * @ClassName: AddStudentAdapter
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/2 9:27
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/2 9:27
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AddStudentAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private Context mContext;
    private List<ContentDataBean> contentDataBeans;
    private boolean isEdit = false;

    public AddStudentAdapter(Context mContext, List<ContentDataBean> contentDataBeans, boolean isEdit) {
        this.mContext = mContext;
        this.contentDataBeans = contentDataBeans;
        this.isEdit = isEdit;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        AdapterHeadViewBinding adapterHeadViewBinding = null;
        if (convertView == null) {
            adapterHeadViewBinding = AdapterHeadViewBinding.inflate(LayoutInflater.from(mContext));
            convertView = adapterHeadViewBinding.getRoot();
            convertView.setTag(adapterHeadViewBinding);
        } else {
            adapterHeadViewBinding = (AdapterHeadViewBinding) convertView.getTag();
        }
        adapterHeadViewBinding.tv.setText(contentDataBeans.get(position).getGroupTitle());
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return contentDataBeans.get(position).getGroupTitle().subSequence(0, 1).charAt(0);
    }

    @Override
    public int getCount() {
        return contentDataBeans == null ? 0 : contentDataBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return contentDataBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public int getViewTypeCount() {
        return 8;
    }

    @Override
    public int getItemViewType(int position) {
        if (contentDataBeans.get(position).getType() == ContentType.SINGLE_LINE_TEXT) {
            return 0;
        } else if (contentDataBeans.get(position).getType() == ContentType.RADIO) {
            return 1;
        } else if (contentDataBeans.get(position).getType() == ContentType.CHOICE_TV) {
            return 2;
        } else if (contentDataBeans.get(position).getType() == ContentType.TIME_TV) {
            return 3;
        } else if (contentDataBeans.get(position).getType() == ContentType.VISION_TEXT) {
            return 4;
        } else if (contentDataBeans.get(position).getType() == ContentType.HIS_VISION_TEXT) {
            return 5;
        }else if (contentDataBeans.get(position).getType() == ContentType.SCHOOL_CHOICE_TV) {
            return 6;
        }
        return 7;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = new EditContentView(mContext);
                    break;
                case 1:
                    convertView = new RadioContentView(mContext);
                    break;
                case 2:
                    convertView = new ChoiceContentView(mContext);
                    break;
                case 3:
                    convertView = new TimeContentView(mContext);
                    break;
                case 4:
                    convertView = new VisionContentView(mContext);
                    break;
                case 5:
                    convertView = new HistoryVisionContentView(mContext);
                    break;
                case 6:
                    convertView = new SchoolChoiceContentView(mContext);
                    break;
                default:
            }
        }

        BaseContentView baseContentView = (BaseContentView) convertView;
        baseContentView.upStatus(isEdit);
        baseContentView.setContentDataBean(contentDataBeans.get(position));
        return convertView;
    }
}
